{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3 数据索引和切片"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.Series对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:30.955370Z",
     "end_time": "2024-05-08T19:46:31.493017Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "g = np.array([27466.15, 24899.3, 19610.9, 19492.4, 17885.39, 17558.76, 15475.09, 12170.2])\n",
    "gdp = pd.Series(g, index=['shanghai', 'beijing', 'guangzhou', 'shenzhen', 'tianjin', 'chongqing', 'suzhou', 'chengdu'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.499087Z",
     "end_time": "2024-05-08T19:46:31.514898Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "15475.09"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp['suzhou']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.519509Z",
     "end_time": "2024-05-08T19:46:31.568891Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'shanghai' in gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.542338Z",
     "end_time": "2024-05-08T19:46:31.578257Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hangzhou' in gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.574388Z",
     "end_time": "2024-05-08T19:46:31.643128Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['shanghai', 'beijing', 'guangzhou', 'shenzhen', 'tianjin', 'chongqing',\n       'suzhou', 'chengdu'],\n      dtype='object')"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.keys()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.597623Z",
     "end_time": "2024-05-08T19:46:31.652747Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "[('shanghai', 27466.15),\n ('beijing', 24899.3),\n ('guangzhou', 19610.9),\n ('shenzhen', 19492.4),\n ('tianjin', 17885.39),\n ('chongqing', 17558.76),\n ('suzhou', 15475.09),\n ('chengdu', 12170.2)]"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(gdp.items())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.623283Z",
     "end_time": "2024-05-08T19:46:31.652747Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai     27466.15\nbeijing      24899.30\nguangzhou    19610.90\nshenzhen     19492.40\ntianjin      17885.39\nchongqing    17558.76\nsuzhou       15475.09\nchengdu      12170.20\nhangzhou     11050.50\ndtype: float64"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp['hangzhou'] = 11050.5\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.648048Z",
     "end_time": "2024-05-08T19:46:31.713430Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "15475.09"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.suzhou"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.672335Z",
     "end_time": "2024-05-08T19:46:31.723310Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "2.1643773445057337"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randn(4), index=[\"tot\", \"pop\", \"sos\", \"mom\"])\n",
    "s.tot"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.692308Z",
     "end_time": "2024-05-08T19:46:31.723310Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "<bound method Series.pop of tot    2.164377\npop   -0.353471\nsos    0.555676\nmom   -1.240377\ndtype: float64>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.pop"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.718023Z",
     "end_time": "2024-05-08T19:46:31.789607Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "suzhou      15475.09\nshanghai    27466.15\nbeijing     24899.30\ndtype: float64"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp[['suzhou', 'shanghai', 'beijing']]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.743857Z",
     "end_time": "2024-05-08T19:46:31.796733Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai    27466.15\nbeijing     24899.30\ndtype: float64"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp[gdp > 20000]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.770673Z",
     "end_time": "2024-05-08T19:46:31.801303Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "tianjin      17885.39\nchongqing    17558.76\nsuzhou       15475.09\ndtype: float64"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = gdp['tianjin': 'suzhou']\n",
    "g"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.791567Z",
     "end_time": "2024-05-08T19:46:31.839060Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "tianjin      17885.39\nchongqing    17558.76\nsuzhou       15475.09\nwuhan        11912.60\ndtype: float64"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g['wuhan'] = 11912.6\n",
    "g"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.818360Z",
     "end_time": "2024-05-08T19:46:31.839060Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai     27466.15\nbeijing      24899.30\nguangzhou    19610.90\nshenzhen     19492.40\ntianjin      17885.39\nchongqing    17558.76\nsuzhou       15475.09\nchengdu      12170.20\nhangzhou     11050.50\ndtype: float64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.831960Z",
     "end_time": "2024-05-08T19:46:31.878681Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "19610.9"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.iloc[2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.868849Z",
     "end_time": "2024-05-08T19:46:31.878681Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "guangzhou    19610.90\nshenzhen     19492.40\ntianjin      17885.39\nchongqing    17558.76\ndtype: float64"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp[2:6]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.878681Z",
     "end_time": "2024-05-08T19:46:31.900360Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "1   -1.298080\n3   -2.979016\n5   -0.074586\n7    0.002644\ndtype: float64"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randn(4), index=[1, 3, 5, 7])\n",
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.905684Z",
     "end_time": "2024-05-08T19:46:31.976463Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "-1.29808047096514"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.935148Z",
     "end_time": "2024-05-08T19:46:31.993800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "3   -2.979016\n5   -0.074586\ndtype: float64"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[1:3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.954639Z",
     "end_time": "2024-05-08T19:46:31.993800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "-2.9790155381987344"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.iloc[1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.982172Z",
     "end_time": "2024-05-08T19:46:32.004209Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "3   -2.979016\n5   -0.074586\ndtype: float64"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.iloc[1:3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:31.993800Z",
     "end_time": "2024-05-08T19:46:32.073188Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "1   -1.298080\n3   -2.979016\ndtype: float64"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.loc[1:3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.021472Z",
     "end_time": "2024-05-08T19:46:32.083801Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai   2015    25300\n           2016    27466\nbeijing    2015    23000\n           2016    24899\nguangzhou  2015    18100\n           2016    19611\ndtype: int64"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp_index = [(\"shanghai\", 2015), (\"shanghai\", 2016), (\"beijing\", 2015), (\"beijing\", 2016), (\"guangzhou\", 2015),\n",
    "             (\"guangzhou\", 2016)]\n",
    "gdp_mind = pd.MultiIndex.from_tuples(gdp_index)\n",
    "gdp = pd.Series([25300, 27466, 23000, 24899, 18100, 19611], index=gdp_mind)\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.037220Z",
     "end_time": "2024-05-08T19:46:32.083801Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "pandas.core.series.Series"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g1 = gdp['shanghai']\n",
    "type(g1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.052035Z",
     "end_time": "2024-05-08T19:46:32.083801Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "2015    25300\n2016    27466\ndtype: int64"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.079481Z",
     "end_time": "2024-05-08T19:46:32.133440Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "2015    25300\n2016    27466\ndtype: int64"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.loc[\"shanghai\"]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.104630Z",
     "end_time": "2024-05-08T19:46:32.133440Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "25300"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.iloc[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.114810Z",
     "end_time": "2024-05-08T19:46:32.156326Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "25300"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.loc['shanghai', 2015]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.129353Z",
     "end_time": "2024-05-08T19:46:32.156326Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai     25300\nbeijing      23000\nguangzhou    18100\ndtype: int64"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.loc[:, 2015]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.150349Z",
     "end_time": "2024-05-08T19:46:32.215016Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai   2015    25300\n           2016    27466\nbeijing    2015    23000\n           2016    24899\nguangzhou  2016    19611\ndtype: int64"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.loc[gdp > 19000]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.179383Z",
     "end_time": "2024-05-08T19:46:32.257180Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "array([25300, 27466, 23000, 24899, 18100, 19611], dtype=int64)"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.values"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.193722Z",
     "end_time": "2024-05-08T19:46:32.275932Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai   2016    27466\nbeijing    2015    23000\n           2016    24899\nguangzhou  2015    18100\ndtype: int64"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.iloc[1:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.209575Z",
     "end_time": "2024-05-08T19:46:32.275932Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai   2016    27466\nbeijing    2016    24899\nguangzhou  2016    19611\ndtype: int64"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.iloc[[1, 3, 5]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.234107Z",
     "end_time": "2024-05-08T19:46:32.314836Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.DataFrame对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "             gdp      pop\nshanghai   27466  2415.27\nbeijing    24899  2151.60\nguangzhou  19611  1270.08",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>gdp</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>shanghai</th>\n      <td>27466</td>\n      <td>2415.27</td>\n    </tr>\n    <tr>\n      <th>beijing</th>\n      <td>24899</td>\n      <td>2151.60</td>\n    </tr>\n    <tr>\n      <th>guangzhou</th>\n      <td>19611</td>\n      <td>1270.08</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "population = pd.Series([2415.27, 2151.6, 1270.08], index=[\"shanghai\", \"beijing\", \"guangzhou\"])\n",
    "gdp = pd.Series([27466, 24899, 19611], index=[\"shanghai\", \"beijing\", \"guangzhou\"])\n",
    "d = pd.DataFrame({'gdp': gdp, 'pop': population})\n",
    "d"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.265958Z",
     "end_time": "2024-05-08T19:46:32.314836Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[27466.  ,  2415.27],\n       [24899.  ,  2151.6 ],\n       [19611.  ,  1270.08]])"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.values"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.293523Z",
     "end_time": "2024-05-08T19:46:32.314836Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "array([27466.  ,  2415.27])"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.values[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.303244Z",
     "end_time": "2024-05-08T19:46:32.337307Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "     shanghai  beijing  guangzhou\ngdp  27466.00  24899.0   19611.00\npop   2415.27   2151.6    1270.08",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>shanghai</th>\n      <th>beijing</th>\n      <th>guangzhou</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>gdp</th>\n      <td>27466.00</td>\n      <td>24899.0</td>\n      <td>19611.00</td>\n    </tr>\n    <tr>\n      <th>pop</th>\n      <td>2415.27</td>\n      <td>2151.6</td>\n      <td>1270.08</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.334787Z",
     "end_time": "2024-05-08T19:46:32.411994Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai     27466\nbeijing      24899\nguangzhou    19611\nName: gdp, dtype: int64"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['gdp']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.368034Z",
     "end_time": "2024-05-08T19:46:32.444736Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "gdp    24899.0\npop     2151.6\nName: beijing, dtype: float64"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.iloc[1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.391462Z",
     "end_time": "2024-05-08T19:46:32.515311Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "2151.6"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.iloc[1, 1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.417753Z",
     "end_time": "2024-05-08T19:46:32.562721Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "             gdp      pop\nbeijing    24899  2151.60\nguangzhou  19611  1270.08",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>gdp</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>beijing</th>\n      <td>24899</td>\n      <td>2151.60</td>\n    </tr>\n    <tr>\n      <th>guangzhou</th>\n      <td>19611</td>\n      <td>1270.08</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.iloc[1:3, :2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.444736Z",
     "end_time": "2024-05-08T19:46:32.615667Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "2151.6"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.loc['beijing', 'pop']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.459355Z",
     "end_time": "2024-05-08T19:46:32.625224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "data": {
      "text/plain": "beijing      2151.60\nguangzhou    1270.08\nName: pop, dtype: float64"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.loc['beijing':'guangzhou', 'pop']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.483548Z",
     "end_time": "2024-05-08T19:46:32.625224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "data": {
      "text/plain": "             gdp      pop\nbeijing    24899  2151.60\nguangzhou  19611  1270.08",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>gdp</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>beijing</th>\n      <td>24899</td>\n      <td>2151.60</td>\n    </tr>\n    <tr>\n      <th>guangzhou</th>\n      <td>19611</td>\n      <td>1270.08</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.loc['beijing':'guangzhou', 'gdp':'pop']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.510787Z",
     "end_time": "2024-05-08T19:46:32.625224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai     2415.27\nbeijing      2151.60\nguangzhou    1270.08\nName: pop, dtype: float64"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['pop']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.526449Z",
     "end_time": "2024-05-08T19:46:32.625224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "             gdp      pop\nbeijing    24899  2151.60\nguangzhou  19611  1270.08",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>gdp</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>beijing</th>\n      <td>24899</td>\n      <td>2151.60</td>\n    </tr>\n    <tr>\n      <th>guangzhou</th>\n      <td>19611</td>\n      <td>1270.08</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['beijing':'guangzhou']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.554638Z",
     "end_time": "2024-05-08T19:46:32.625224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/plain": "             gdp      pop\nbeijing    24899  2151.60\nguangzhou  19611  1270.08",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>gdp</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>beijing</th>\n      <td>24899</td>\n      <td>2151.60</td>\n    </tr>\n    <tr>\n      <th>guangzhou</th>\n      <td>19611</td>\n      <td>1270.08</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.loc['beijing':'guangzhou']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.584247Z",
     "end_time": "2024-05-08T19:46:32.625224Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "             gdp      pop\nbeijing    24899  2151.60\nguangzhou  19611  1270.08",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>gdp</th>\n      <th>pop</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>beijing</th>\n      <td>24899</td>\n      <td>2151.60</td>\n    </tr>\n    <tr>\n      <th>guangzhou</th>\n      <td>19611</td>\n      <td>1270.08</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.iloc[1:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.599190Z",
     "end_time": "2024-05-08T19:46:32.708710Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "2415.27"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.loc['shanghai', 'pop']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.617222Z",
     "end_time": "2024-05-08T19:46:32.766336Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "data": {
      "text/plain": "shanghai     2415.27\nbeijing      2151.60\nguangzhou    1270.08\nName: pop, dtype: float64"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.loc[:, 'pop']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.645241Z",
     "end_time": "2024-05-08T19:46:32.839008Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "outputs": [
    {
     "data": {
      "text/plain": "name        Hertz        Newton          Sola      \nsubject   Chinese   Phy Chinese   Phy Chinese   Phy\nyear test                                          \n2016 1       66.0  72.0    68.0  74.0    76.0  58.0\n     2       63.0  66.0    74.0  85.0    62.0  68.0\n2017 1       76.0  92.0    87.0  72.0    84.0  73.0\n     2       68.0  61.0    78.0  57.0    73.0  80.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>name</th>\n      <th colspan=\"2\" halign=\"left\">Hertz</th>\n      <th colspan=\"2\" halign=\"left\">Newton</th>\n      <th colspan=\"2\" halign=\"left\">Sola</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>subject</th>\n      <th>Chinese</th>\n      <th>Phy</th>\n      <th>Chinese</th>\n      <th>Phy</th>\n      <th>Chinese</th>\n      <th>Phy</th>\n    </tr>\n    <tr>\n      <th>year</th>\n      <th>test</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">2016</th>\n      <th>1</th>\n      <td>66.0</td>\n      <td>72.0</td>\n      <td>68.0</td>\n      <td>74.0</td>\n      <td>76.0</td>\n      <td>58.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>63.0</td>\n      <td>66.0</td>\n      <td>74.0</td>\n      <td>85.0</td>\n      <td>62.0</td>\n      <td>68.0</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">2017</th>\n      <th>1</th>\n      <td>76.0</td>\n      <td>92.0</td>\n      <td>87.0</td>\n      <td>72.0</td>\n      <td>84.0</td>\n      <td>73.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>68.0</td>\n      <td>61.0</td>\n      <td>78.0</td>\n      <td>57.0</td>\n      <td>73.0</td>\n      <td>80.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mind = pd.MultiIndex.from_product([[2016, 2017], [1, 2]],\n",
    "                                  names=[\"year\", \"test\"])\n",
    "columns = pd.MultiIndex.from_product([['Hertz', 'Newton', 'Sola'], ['Chinese', 'Phy']], names=['name', 'subject'])\n",
    "data = np.round(np.random.randn(4, 6), 1)\n",
    "data = data * 10 + 70\n",
    "scores = pd.DataFrame(data, index=mind, columns=columns)\n",
    "scores"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.668846Z",
     "end_time": "2024-05-08T19:46:33.016301Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "data": {
      "text/plain": "subject    Chinese   Phy\nyear test               \n2016 1        66.0  72.0\n     2        63.0  66.0\n2017 1        76.0  92.0\n     2        68.0  61.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>Chinese</th>\n      <th>Phy</th>\n    </tr>\n    <tr>\n      <th>year</th>\n      <th>test</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">2016</th>\n      <th>1</th>\n      <td>66.0</td>\n      <td>72.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>63.0</td>\n      <td>66.0</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">2017</th>\n      <th>1</th>\n      <td>76.0</td>\n      <td>92.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>68.0</td>\n      <td>61.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores['Hertz']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.701669Z",
     "end_time": "2024-05-08T19:46:33.026766Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "data": {
      "text/plain": "year  test\n2016  1       72.0\n      2       66.0\n2017  1       92.0\n      2       61.0\nName: (Hertz, Phy), dtype: float64"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores['Hertz', 'Phy']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.717376Z",
     "end_time": "2024-05-08T19:46:33.084179Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "year  test\n2016  1       58.0\n      2       68.0\n2017  1       73.0\n      2       80.0\nName: (Sola, Phy), dtype: float64"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.loc[:, ('Sola', 'Phy')]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.743458Z",
     "end_time": "2024-05-08T19:46:33.116379Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "data": {
      "text/plain": "name        Hertz        Newton          Sola      \nsubject   Chinese   Phy Chinese   Phy Chinese   Phy\nyear test                                          \n2016 2       63.0  66.0    74.0  85.0    62.0  68.0\n2017 1       76.0  92.0    87.0  72.0    84.0  73.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>name</th>\n      <th colspan=\"2\" halign=\"left\">Hertz</th>\n      <th colspan=\"2\" halign=\"left\">Newton</th>\n      <th colspan=\"2\" halign=\"left\">Sola</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>subject</th>\n      <th>Chinese</th>\n      <th>Phy</th>\n      <th>Chinese</th>\n      <th>Phy</th>\n      <th>Chinese</th>\n      <th>Phy</th>\n    </tr>\n    <tr>\n      <th>year</th>\n      <th>test</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2016</th>\n      <th>2</th>\n      <td>63.0</td>\n      <td>66.0</td>\n      <td>74.0</td>\n      <td>85.0</td>\n      <td>62.0</td>\n      <td>68.0</td>\n    </tr>\n    <tr>\n      <th>2017</th>\n      <th>1</th>\n      <td>76.0</td>\n      <td>92.0</td>\n      <td>87.0</td>\n      <td>72.0</td>\n      <td>84.0</td>\n      <td>73.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.iloc[1:3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.770375Z",
     "end_time": "2024-05-08T19:46:33.131289Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "outputs": [
    {
     "data": {
      "text/plain": "name    subject\nHertz   Chinese    63.0\n        Phy        66.0\nNewton  Chinese    74.0\n        Phy        85.0\nSola    Chinese    62.0\n        Phy        68.0\nName: (2016, 2), dtype: float64"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.loc[2016, 2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.786540Z",
     "end_time": "2024-05-08T19:46:33.158784Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "subject\nChinese    74.0\nPhy        85.0\nName: (2016, 2), dtype: float64"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.loc[(2016, 2), ('Newton')]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.812979Z",
     "end_time": "2024-05-08T19:46:33.160459Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "data": {
      "text/plain": "name      Hertz Newton  Sola\nsubject     Phy    Phy   Phy\nyear test                   \n2016 1     72.0   74.0  58.0\n2017 1     92.0   72.0  73.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th>name</th>\n      <th>Hertz</th>\n      <th>Newton</th>\n      <th>Sola</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>subject</th>\n      <th>Phy</th>\n      <th>Phy</th>\n      <th>Phy</th>\n    </tr>\n    <tr>\n      <th>year</th>\n      <th>test</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2016</th>\n      <th>1</th>\n      <td>72.0</td>\n      <td>74.0</td>\n      <td>58.0</td>\n    </tr>\n    <tr>\n      <th>2017</th>\n      <th>1</th>\n      <td>92.0</td>\n      <td>72.0</td>\n      <td>73.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx = pd.IndexSlice\n",
    "scores.loc[idx[:, 1], idx[:, \"Phy\"]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.844852Z",
     "end_time": "2024-05-08T19:46:33.176960Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [],
   "source": [
    "columns = pd.MultiIndex.from_product([['Hertz', 'Newton', 'Sola'], ['Physics', 'Chinese']], names=['name', 'subject'])\n",
    "scores = pd.DataFrame(data, index=mind, columns=columns)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.883090Z",
     "end_time": "2024-05-08T19:46:33.176960Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T19:46:32.909157Z",
     "end_time": "2024-05-08T19:46:33.176960Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
